using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using Xunit;
namespace TestProject1;

public class UnitTest1
{
    //[Fact]
    //public void Test1()
    //{

    //}
    public class OrderServiceTests
    {
        [Fact]
        public void AddOrder_NewOrder_ShouldBeAdded()
        {
            // Arrange
            OrderService orderService = new OrderService();
            Order order = new Order { OrderId = 1, Customer = "Customer1" };

            // Act
            orderService.AddOrder(order);

            // Assert
            Assert.Contains(order, orderService.QueryOrders(o => true));
        }

        [Fact]
        public void AddOrder_ExistingOrder_ShouldThrowException()
        {
            // Arrange
            OrderService orderService = new OrderService();
            Order order = new Order { OrderId = 1, Customer = "Customer1" };
            orderService.AddOrder(order);

            // Act & Assert
            Assert.Throws<InvalidCastException>(() => orderService.AddOrder(order));
        }

        [Fact]
        public void RemoveOrder_ExistingOrderId_ShouldBeRemoved()
        {
            // Arrange
            OrderService orderService = new OrderService();
            Order order = new Order { OrderId = 1, Customer = "Customer1" };
            orderService.AddOrder(order);

            // Act
            orderService.RemoveOrder(1);

            // Assert
            Assert.DoesNotContain(order, orderService.QueryOrders(o => true));
        }

        [Fact]
        public void RemoveOrder_NonExistingOrderId_ShouldThrowException()
        {
            // Arrange
            OrderService orderService = new OrderService();

            // Act & Assert
            Assert.Throws<KeyNotFoundException>(() => orderService.RemoveOrder(1));
        }

        [Fact]
        public void QueryOrders_FilterByCustomer_ShouldReturnFilteredOrders()
        {
            // Arrange
            OrderService orderService = new OrderService();
            Order order1 = new Order { OrderId = 1, Customer = "Customer1" };
            Order order2 = new Order { OrderId = 2, Customer = "Customer2" };
            orderService.AddOrder(order1);
            orderService.AddOrder(order2);

            // Act
            List<Order> filteredOrders = orderService.QueryOrders(o => o.Customer == "Customer1");

            // Assert
            Assert.Contains(order1, filteredOrders);
            Assert.DoesNotContain(order2, filteredOrders);
        }

        [Fact]
        public void SortOrders_SortByOrderId_ShouldReturnSortedOrders()
        {
            // Arrange
            OrderService orderService = new OrderService();
            Order order1 = new Order { OrderId = 2, Customer = "Customer2" };
            Order order2 = new Order { OrderId = 1, Customer = "Customer1" };
            orderService.AddOrder(order1);
            orderService.AddOrder(order2);

            // Act
            orderService.SortOrders(o => o.OrderId);

            // Assert
            List<Order> sortedOrders = orderService.QueryOrders(o => true);
            Assert.Equal(order2, sortedOrders[0]);
            Assert.Equal(order1, sortedOrders[1]);
        }

        [Fact]
        public void Export_OrdersToXml_FileShouldExist()
        {
            // Arrange
            OrderService orderService = new OrderService();
            Order order1 = new Order { OrderId = 1, Customer = "Customer1" };
            orderService.AddOrder(order1);
            string filePath = "test_export.xml";

            // Act
            orderService.Export(filePath);

            // Assert
            Assert.True(File.Exists(filePath));

            // Clean up
            File.Delete(filePath);
        }

        [Fact]
        public void Import_FromXmlFile_ShouldContainOrders()
        {
            // Arrange
            OrderService orderService = new OrderService();
            Order order1 = new Order { OrderId = 1, Customer = "Customer1" };
            orderService.AddOrder(order1);
            string filePath = "test_import.xml";
            orderService.Export(filePath);

            // Act
            orderService = new OrderService();
            orderService.Import(filePath);

            // Assert
            Assert.NotEmpty(orderService.QueryOrders(o => true));

            // Clean up
            File.Delete(filePath);
        }
    }
}